package com.RMP
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import com.greensock.*;

    public class ProjThumbnail extends Sprite
    {
        public var myXMLList:XML;
        var loader:Loader;
        var projectId:Object;
        var myURL:String;
        public var showIcon:Boolean = true;
        var fadeSpeed:Number;
        public var asIconHover:Boolean = false;
        var bg:Sprite;
        public var orient:String;
        var btnOver:Sprite;
        var image:Bitmap;
        var frame:Frame;
        var myRoot:MovieClip;
        public var asIcon:AddSubtractIcon;
        public var myId:int;

        public function ProjThumbnail(param1:MovieClip, param2:Number, param3:XML, param4:int, param5) : void
        {
            asIconHover = false;
            showIcon = true;
            alpha = 0;
            visible = false;
            myRoot = param1;
            fadeSpeed = param2;
            myXMLList = param3;
            myId = param4;
            projectId = param5;
            btnOver = new Sprite();
            btnOver.useHandCursor = false;
            btnOver.mouseEnabled = false;
            btnOver.buttonMode = false;
            btnOver.graphics.beginFill(16777215, 100);
            btnOver.graphics.drawRect(0, 0, 1, 1);
            btnOver.graphics.endFill();
            btnOver.alpha = 0;
            loader = new Loader();
            if (myXMLList.@tUrl == undefined)
            {
                loader.load(new URLRequest(myRoot.path + myXMLList.@still));
            }
            else
            {
                loader.load(new URLRequest(myRoot.path + myXMLList.@tUrl));
            }
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
            myRoot.stage.addEventListener(Event.RESIZE, onResizer, false, 0, true);
            doResize();
            return;
        }

        public function fadeOutShell() : void
        {
            if (image != null)
            {
                image.smoothing = false;
            }
            TweenMax.to(this, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                visible = false;
                return;
            }
            });
            return;
        }

        public function rollOverEvent() : void
        {
            if (asIcon != null)
            {
                if (myRoot.localImagesXML.image.@id.contains(myXMLList.@id))
                {
                    asIcon.inLightbox = true;
                }
                else
                {
                    asIcon.inLightbox = false;
                }
                if (asIcon.inLightbox == false)
                {
                    asIcon.plus.alpha = 1;
                    asIcon.minus.alpha = 0;
                }
                else
                {
                    asIcon.plus.alpha = 0;
                    asIcon.minus.alpha = 1;
                }
                asIcon.alpha = 1;
            }
            TweenMax.to(btnOver, fadeSpeed, {alpha:0.5, overwrite:true});
            return;
        }

        function asOver(event:MouseEvent)
        {
            asIconHover = true;
            return;
        }

        function doResize() : void
        {
            if (image != null)
            {
                var _loc_1:* = myRoot.unitW / 75;
                image.scaleY = myRoot.unitW / 75;
                image.scaleX = _loc_1;
                image.width = Math.round(image.width);
                image.height = Math.round(image.height);
                btnOver.width = image.width;
                btnOver.height = image.height;
                if (frame != null)
                {
                    frame.resizeFrame(image.width, image.height);
                }
                if (asIcon != null)
                {
                    asIcon.x = 0;
                    asIcon.y = 0;
                }
            }
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        function asClick(event:MouseEvent)
        {
            myRoot.updateBookmark();
            if (asIcon.inLightbox == false)
            {
                myRoot.removeFromBookmarks(projectId, myXMLList.@id);
            }
            else
            {
                myRoot.addToBookmarks(projectId, myXMLList.@id);
            }
            return;
        }

        public function rollOutEvent() : void
        {
            if (asIcon != null)
            {
                asIcon.alpha = 0;
            }
            TweenMax.to(btnOver, fadeSpeed * 3, {alpha:0, overwrite:true});
            return;
        }

        function onLoadComplete(event:Event) : void
        {
            var event:* = event;
            image = Bitmap(loader.content);
            doResize();
            image.alpha = 0;
            addChild(image);
            if (myXMLList.@tBorder == "1" || myXMLList.@tborder == "1")
            {
                frame = new Frame();
                frame.alpha = 0;
                addChild(frame);
                frame.resizeFrame(image.width, image.height);
                TweenMax.to(frame, fadeSpeed, {alpha:1, overwrite:true});
            }
            addChild(btnOver);
            TweenMax.to(image, fadeSpeed, {alpha:1, overwrite:true, onComplete:function ()
            {
                image.smoothing = true;
                return;
            }
            });
            if (showIcon == true)
            {
                asIcon = new AddSubtractIcon(myRoot, fadeSpeed);
                asIcon.buttonMode = true;
                asIcon.useHandCursor = true;
                asIcon.mouseChildren = true;
                asIcon.alpha = 0;
                asIcon.addEventListener(MouseEvent.MOUSE_OVER, asOver);
                asIcon.addEventListener(MouseEvent.MOUSE_OUT, asOut);
                asIcon.addEventListener(MouseEvent.CLICK, asClick);
                if (myRoot.localImagesXML.image.@id.contains(myXMLList.@id))
                {
                    asIcon.inLightbox = true;
                }
                else
                {
                    asIcon.inLightbox = false;
                }
                addChild(asIcon);
            }
            doResize();
            return;
        }

        function asOut(event:MouseEvent)
        {
            asIconHover = false;
            return;
        }

        public function fadeInShell() : void
        {
            visible = true;
            TweenMax.to(this, fadeSpeed, {alpha:1, delay:fadeSpeed, overwrite:true, onComplete:function ()
            {
                if (image != null)
                {
                    image.smoothing = true;
                }
                return;
            }
            });
            return;
        }

    }
}
